package com.kbook.action.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.kbook.bean.UserBean;
import com.kbook.dao.CategoryDao;
import com.kbook.dao.ChartsDao;
import com.kbook.dao.NovelDao;
import com.kbook.dao.impl.CategoryDaoImpl;
import com.kbook.dao.impl.ChartsDaoImpl;
import com.kbook.dao.impl.NovelDaoImpl;
import com.kbook.log.LogWriter;
import com.kbook.tool.Constants;
import com.kbook.tool.Tools;
import com.kbook.vo.Category;
import com.kbook.vo.Charts;
import com.kbook.vo.Novel;

public class ListAction extends AbstractAction {
	
	private static final Logger logger = Logger.getLogger("ListAction");

	private static final int PAGE_SIZE=24;
	
	@Override
	protected Object doAction(UserBean user, Map params) throws Exception {
		// TODO Auto-generated method stub
		String categoryId = (String)params.get("ct");
		String page = (String)params.get("page");
		int int_page = Tools.parseInt(page);
		if(int_page==-1){
			int_page=1;
		}
		String status=(String)params.get("status");
		short short_status = Tools.parseShort(status);
		
		short short_categoryId = Tools.parseShort(categoryId);
		CategoryDao categoryDao = new CategoryDaoImpl();
		Category category = categoryDao.query(short_categoryId);
		
		int totalNum = category.getTotalNum();
		
		int totalPage = Tools.getTotalPage(totalNum, PAGE_SIZE);
		if(int_page > totalPage){
			int_page = totalPage;
		}
		
		int[] pageInfo = Tools.getPageInfo(PAGE_SIZE, int_page, totalNum);
		
		NovelDao novelDao = new NovelDaoImpl();
		List<Novel> novelList = null;
		if(short_status == -1){
			//System.out.println("categoryId="+ short_categoryId+"; pageInfo[0]="+pageInfo[0]+"; pagesize="+PAGE_SIZE);
			novelList = novelDao.queryNovelByCategoryId(short_categoryId, pageInfo[0], PAGE_SIZE);
			//System.out.println("pageInfo[0]=" + pageInfo[0] + ";  pageInfo[1]=" +  pageInfo[1]);
		}else{
			novelList = novelDao.queryNovelByCategoryAndStatus(short_categoryId, short_status, pageInfo[0], PAGE_SIZE);
		}
			
		List<Category> categoryList = categoryDao.queryAll();
		ChartsDao chartDao = new ChartsDaoImpl();
		List<Charts> hotestChartList = chartDao.queryById(Constants.CHART_HOTEST);
		List<Charts> newestChartList = chartDao.queryById(Constants.CHART_NEWEST);
		
		Map resultMap = new HashMap();
		resultMap.put("page", int_page);
		resultMap.put("totalNum", totalNum);
		resultMap.put("novelList", novelList);
		resultMap.put("categoryList", categoryList);
		resultMap.put("category", category);
		resultMap.put("hotestChartList", hotestChartList);
		resultMap.put("newestChartList", newestChartList);
		resultMap.put("totalPage", totalPage);
		
		return resultMap;
	}

	@Override
	protected void afterAction(Object obj, UserBean user, Map params) {
		// TODO Auto-generated method stub
		List<String> logParams = new ArrayList<String>();
		logParams.add((String)params.get("ip"));
		logParams.add((String)params.get("ua"));
		logParams.add((String)params.get("cid"));
		logParams.add((String)params.get("ct"));
		logParams.add((String)params.get("page"));
		logParams.add((String)params.get("status"));

		LogWriter.getInstance().createLog(logParams, logger);
	}

	
	
}
